<template>
    <div id="add-edit-fun">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="769px" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="auto" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('key_62')" prop="name">
                            <el-input v-model="ruleForm.name" :placeholder="$t('key_70')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('key_71')" prop="menuID">
                            <!-- <el-input v-model="ruleForm.menuID" placeholder="请输入功能名称" clearable /> -->
                            <el-select 
                                v-model="ruleForm.menuID" 
                                filterable class="m-2" 
                                :placeholder="$t('key_72')" 
                                size="large"
                                @change="menuChange"
                                value-key="id"
                            >
                                <el-option
                                    v-for="item in menuOptions"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.jobNum"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('key_73')" prop="disable">
                            <el-switch
                                v-model="ruleForm.disable"
                                active-color="#13ce66"
                                inactive-color="#ff4949">
                            </el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('key_74')" prop="description">
                            <el-input v-model="ruleForm.description" :placeholder="$t('key_75')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('key_76')" prop="afterTask">
                            <el-input type="textarea" v-model="ruleForm.afterTask" :placeholder="$t('key_77')" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">{{ $t('key_24') }}</el-button>
                <el-button type="primary" :disabled="repeatDis" @click="submitForm(ruleFormRef)">{{ $t('key_25') }}</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch, Ref } from 'vue'
import { FormInstance, FormRules , ElMessage, ElButton, ElCol, ElDatePicker, ElDialog, ElDivider, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import { sysCheckfuncAdd , sysCheckfuncEdit } from '/@/api/manage/manage.ts'
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    id: String, // 主键id
    name: String, // 审核功能名称
    menuID: String, // 菜单id
    disable: Boolean, // 是否启用
    description: '', // 描述
    afterTask: '', // 操作
}
export default defineComponent( {
    name: 'add-edit-fun',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef:Ref = ref()
        const menuOptions = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            name: [
				{ required: true, message: window.$t('key_78'), trigger: 'blur' },
			],
        })

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            ctx.emit('cancelUseClick')
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    let setObj = toRaw(state.ruleForm)
                    if(setObj.id === ''){
                        const {id , ...param} = setObj
                        console.log(param,'param')
                        let res = await sysCheckfuncAdd(param)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick')
                        }
                    }else{
                        let res = await sysCheckfuncEdit(setObj)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick')
                        }
                    }
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** menuChange 菜单下拉改变事件 */
        const menuChange = (val:any) => {
            
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                state.repeatDis = false
                if(props.repairInfo !== ''){
                    titleCon.value = window.$t('key_8')
                    console.log(JSON.parse(props.repairInfo),'repairInfo')
                    state.ruleForm.id = JSON.parse(props.repairInfo).id
                    state.ruleForm.name = JSON.parse(props.repairInfo).name // 审核功能名称
                    state.ruleForm.menuID = JSON.parse(props.repairInfo).menuID ? JSON.parse(props.repairInfo).menuID : '' // 菜单id
                    state.ruleForm.disable = JSON.parse(props.repairInfo).disable ? false : true // 是否启用
                    state.ruleForm.description = JSON.parse(props.repairInfo).description ? JSON.parse(props.repairInfo).description : '' // 描述
                    state.ruleForm.afterTask = JSON.parse(props.repairInfo).afterTask ? JSON.parse(props.repairInfo).afterTask : '' // 操作
                }else{
                    titleCon.value = window.$t('xinZeng')
                    state.ruleForm.id = ''
                    state.ruleForm.name = '' // 审核功能名称
                    state.ruleForm.menuID = '' // 菜单id
                    state.ruleForm.disable = false // 是否启用
                    state.ruleForm.description = '' // 描述
                    state.ruleForm.afterTask = '' // 操作
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
            menuOptions,
            menuChange,
        }
    },
} )
</script>
<style scoped lang="scss">
    .m-2{
        width: 100%;
        :deep(.el-input__inner){
            height: 30px;
        }
    }
</style>